/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.business.sec.rbac.db;

import han.bifang.business.dao.GroupDao;
import han.bifang.business.dao.GroupRoleRelDao;
import han.bifang.business.dao.RoleDao;
import han.bifang.business.dao.UserDao;
import han.bifang.business.dao.UserGroupRelDao;
import han.bifang.business.sec.rbac.GroupProvider;
import han.bifang.business.sync.SyncUtils;
import han.bifang.common.entity.SoaRole;
import han.bifang.common.entity.SoaUsergroup;
import han.bifang.common.entity.SoaUsergroupRoleRel;
import han.bifang.common.entity.SoaUsergroupRoleRelPK;
import han.bifang.common.entity.SoaUsergroupUserRel;
import han.bifang.common.entity.SoaUsergroupUserRelPK;
import han.bifang.common.exception.BifangException;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Kent
 */
public class DbGroupProvider implements GroupProvider {

	@Override
	public void addUsersInGroup(String[] userIds, String groupId)
			throws BifangException {
		ArrayList<SoaUsergroupUserRel> l = new ArrayList<SoaUsergroupUserRel>();
		for(String userId : userIds){
			SoaUsergroupUserRel r = new SoaUsergroupUserRel();
			SoaUsergroupUserRelPK rk = new SoaUsergroupUserRelPK();
			rk.setUsergroupId(groupId);
			rk.setUserId(userId);
			r.setCompId(rk);
			l.add(r);
		}
		UserGroupRelDao.createUsergroupUserRel(l);
		SyncUtils.sync(SyncUtils.SECURITY, null);

	}

	@Override
	public String createGroup(SoaUsergroup group) throws BifangException {
		return GroupDao.createUsergroup(group);
	}

	@Override
	public List findGroupsByPage(String groupName, int page,
			int pageSize) throws BifangException {
		return GroupDao.findGroupsByPage(groupName, page, pageSize);
	}

	

	@Override
	public List<SoaRole> getRolesOfGroup(String groupId) throws BifangException {
		return RoleDao.findRoleByGroupId(groupId);
	}

	@Override
	public List getUsersOfGroupByPage(String groupId, int page,
			int pageSize) throws BifangException {
		
		return UserDao.findUsersOfGroupByPage(groupId, page, pageSize);
	}

	@Override
	public void modifyGroup(SoaUsergroup group) throws BifangException {
		GroupDao.modifyUsergroup(group);
		SyncUtils.sync(SyncUtils.SECURITY, null);

	}

	@Override
	public void removeGroup(String groupId) throws BifangException {
		GroupDao.removeUsergroup(groupId);
		UserGroupRelDao.removeUsergroupUserRelByGroupId(groupId);
		GroupRoleRelDao.removeUsergroupRoleRelByGroupId(groupId);
		SyncUtils.sync(SyncUtils.SECURITY, null);

	}

	@Override
	public void removeUsersFromGroup(String[] userIds, String groupId)
			throws BifangException {
		ArrayList<SoaUsergroupUserRel> l = new ArrayList<SoaUsergroupUserRel>();
		for(String userId : userIds){
			SoaUsergroupUserRel r = new SoaUsergroupUserRel();
			SoaUsergroupUserRelPK rk = new SoaUsergroupUserRelPK();
			rk.setUsergroupId(groupId);
			rk.setUserId(userId);
			r.setCompId(rk);
			l.add(r);
		}
		UserGroupRelDao.removeUsergroupUserRel(l);
		SyncUtils.sync(SyncUtils.SECURITY, null);

	}

	@Override
	public SoaUsergroup getGroupById(String id) throws BifangException {
		return GroupDao.getUsergroup(id);
	}

	@Override
	public SoaUsergroup getGroupByName(String groupName) throws BifangException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void addRolesOfGroup(String[] roleIds, String groupId)
			throws BifangException {
		ArrayList<SoaUsergroupRoleRel> l = new ArrayList<SoaUsergroupRoleRel>();
		for(String roleId : roleIds){
			SoaUsergroupRoleRel r = new SoaUsergroupRoleRel();
			SoaUsergroupRoleRelPK rk = new SoaUsergroupRoleRelPK();
			rk.setUsergroupId(groupId);
			rk.setRoleId(roleId);
			r.setCompId(rk);
			l.add(r);
		}
		GroupRoleRelDao.createUsergroupRoleRel(l);
		SyncUtils.sync(SyncUtils.SECURITY, null);
		
	}

	@Override
	public void removeRolesOfGroup(String[] roleIds, String groupId)
			throws BifangException {
		ArrayList<SoaUsergroupRoleRel> l = new ArrayList<SoaUsergroupRoleRel>();
		for(String roleId : roleIds){
			SoaUsergroupRoleRel r = new SoaUsergroupRoleRel();
			SoaUsergroupRoleRelPK rk = new SoaUsergroupRoleRelPK();
			rk.setUsergroupId(groupId);
			rk.setRoleId(roleId);
			r.setCompId(rk);
			l.add(r);
		}
		GroupRoleRelDao.removeUsergroupRoleRel(l);
		SyncUtils.sync(SyncUtils.SECURITY, null);

		
	}

	@Override
	public List getUsersNotOfGroupByPage(String groupId, String userName, String loginName,int page, int pageSize)
			throws BifangException {
		return UserDao.findUsersNotOfGroupByPage(groupId,userName,loginName, page, pageSize);
	}

	@Override
	public List getRolesNotOfGroupByPage(String groupId, String roleName,
			int page, int pageSize) throws BifangException {
		// TODO Auto-generated method stub
		return RoleDao.findRolesNotOfGroup(groupId, roleName, page, pageSize);
	}

	
	
	
	

}
